const express = require('express')
const app = express()
const port = 3000
const fs = require('node:fs')
const userRouter = require('./src/user.js')
const listRouter = require('./src/list.js')
const { loggerMiddleware } = require('./middleware/logger.js')
// // 获取body参数 使用中间件支持解析json
// app.use(express.json())
app.use(express.json())
// 要放在路由中间件上
app.use(loggerMiddleware)
app.use('/user', userRouter)
app.use('/list', listRouter)
// get 获取参数 req.query
app.get('/api', (req, res) => {
    console.log('req.query', req.query)
    res.setHeader('content-type', 'text/html;carset=utf-8')
    const data = fs.readFileSync('./index.html')
    res.status(200).send(data)
})
// 动态参数
// get 获取动态参数使用 req.params
app.get('/api/:id', (req, res) => {

    console.log('req.params.id', req.params.id)
    res.send('动态参数')
})
// post 获取参数使用 req.body
app.post('/api', (req, res) => {
    console.log('req.body', req.body)
    res.send('NIHAO!')
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))